﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using GroundWater.Data;
using GroundWater.BusinessActions;
using MonitoringTable = GroundWater.Data.MonitoringDataSet.MonitoringDataTable;
using MonitoringRow = GroundWater.Data.MonitoringDataSet.MonitoringRow;

using WellTable = GroundWater.Data.InfoDataSet.WellsDataTable;
using WellRow = GroundWater.Data.InfoDataSet.WellsRow;

namespace GroundWater.analysis
{
    public partial class AverageWaterlevel : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "水井水位及埋深统计");
            if (!this.IsPostBack)
            {
                txtStartDate.Text = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd");
                txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            }

            sm.RegisterAsyncPostBackControl(this.btnAnalysis);
            sm.RegisterAsyncPostBackControl(this.drpWell);
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs args)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gridView.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (args.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        args.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortasc" : " sortdesc");
                    }
                }
            }
        }

        protected void btnAnalysis_Click(object sender, EventArgs e)
        {
            RegisterDataItem();
        }

        protected void drpWell_SelectedIndexChanged(object sender, EventArgs e)
        {
            RegisterDataItem();
        }

        protected void drpWell_DataBound(object sender, EventArgs e)
        {
            
        }

        private string GetWaterlevel()
        {
            if (txtStartDate.Text.Length < 8 || txtEndDate.Text.Length < 8)
                return "";
            DataTable dt = Monitoring.FetchWaterLeveByWellCode(drpWell.SelectedValue, txtStartDate.Text, txtEndDate.Text);
            if (dt == null || dt.Rows.Count == 0)
            {
                return "";
            }

            WellRow well = Well.GetByIdFromCache(drpWell.SelectedValue, false);
            if (well == null)
                return "";
            decimal waterlevel = (decimal)dt.Rows[0][1];
            if (well.Altitude <= waterlevel)
            {
                return waterlevel.ToString();
            }
            decimal tmp = well.Altitude - waterlevel;

            string s = waterlevel.ToString() + "|" + tmp.ToString();

            return s;

        }

        private bool registered = false;

        private void RegisterDataItem()
        {
            string s = GetWaterlevel();
            if (s.Length > 0 && !registered)
            {
                sm.RegisterDataItem(this.btnAnalysis, s);
                registered = !registered;
            }
        }
    }
}
